const { defineConfig } = require('@vue/cli-service');

module.exports = {
  devServer: {
    port: 8080,
    open: true,
    historyApiFallback: true,
    proxy: {
      '/api': {
        target: 'http://localhost:3000',
        changeOrigin: true,
        pathRewrite: { '^/api': '' },
      },
    },
  },
  chainWebpack: config => {
    config.plugin('define').tap(args => {
      args[0]['__VUE_PROD_HYDRATION_MISMATCH_DETAILS__'] = false;
      return args;
    });
    config.resolve.alias
      .set('@', require('path').resolve(__dirname, 'src'))
      .set('src', require('path').resolve(__dirname, 'src'));
  },
  publicPath: process.env.NODE_ENV === 'production' ? '/' : './', // 关键修改
  outputDir: 'dist',
  assetsDir: 'static',
  lintOnSave: process.env.NODE_ENV !== 'production',
  productionSourceMap: false,
  filenameHashing: false,
  // PWA 配置（关键离线功能）
  pwa: {
    name: '闭环管理系统',
    themeColor: '#007077',
    msTileColor: '#007077',
    appleMobileWebAppCapable: 'yes',
    appleMobileWebAppStatusBarStyle: 'black',

    workboxPluginMode: 'GenerateSW', // 使用 Workbox 生成 Service Worker
    workboxOptions: {
      // 预缓存关键资源
      exclude: [/\.map$/, /manifest\.json$/, /service-worker\.js$/],
      runtimeCaching: [
        {
          urlPattern: /\.(?:js|css|html|png|jpe?g|svg|woff2?)$/, // 缓存静态资源
          handler: 'StaleWhileRevalidate',
          options: {
            cacheName: 'static-resources',
            expiration: { maxEntries: 50 }
          }
        }
      ]
    }
  },
};
